﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip;

namespace dkGameTool
{
    public partial class chooseSkillFrm : UserControl
    {
        private string DeviceId;
        private Dictionary<string, Skill> SkillMap;
        private DataTable SelectDt;
        public chooseSkillFrm(string deviceId)
        {
            InitializeComponent();
            SkillMap = new Dictionary<string, Skill>();
            SelectDt = new DataTable();
            this.DeviceId = deviceId;
            DataBaseManager dbHelper = DataBaseManager.GetInstance;

            DataTable dt = dbHelper.getDataSkill("");


            SelectDt.Columns.Add("code", typeof(string));
            SelectDt.Columns.Add("name", typeof(string));
            SelectDt.Columns.Add("weight", typeof(int));


            this.dataGridView1.DataSource = dt;
            this.dataGridView2.DataSource = this.SelectDt;

            loadData(dt);




        }

        private void loadData(DataTable dt)
        {
            // false as chooseSkill, name, code, path, combo
            DataRow[] rows = dt.Select();
            // 显示选择的行
            foreach (DataRow row in rows)
            {
                Skill skill = new Skill();
                object cel2 = row["code"];
                if (cel2 != null)
                {
                    skill.Code = cel2.ToString();
                }

                object cel3 = row["name"];
                if (cel3 != null)
                {
                    skill.Name = cel3.ToString();
                }

                object cel4 = row["combo"];
                if (cel4 != null)
                {
                    skill.Combos = cel4.ToString();
                }

                object cel5 = row["path"];
                if (cel5 != null)
                {
                    skill.ImgPath = cel5.ToString();
                }

                this.SkillMap.Add(skill.Code, skill);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

            DataBaseManager.GetInstance.DelSkillAllRecord(this.DeviceId);

            DataRow[] rows = this.SelectDt.Select();

            foreach (DataRow row in rows)
            {
                string code = row["code"].ToString();


                string sweight = row["weight"].ToString();

                int weight = 0;
                bool success = int.TryParse(sweight, out weight);


                DataBaseManager.GetInstance.AddChooseSkillRecord(this.DeviceId, code, weight);

            }

            this.ParentForm.Close();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.ParentForm.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //
            string path = getCurrImagePath();
            if (string.IsNullOrEmpty(path))
            {
                return;
            }

            dataGridView1.RefreshEdit();

            pictureBox1.Load(path);
        }

        private void combo(string code, bool value)
        {
            foreach (Skill row in this.SkillMap.Values)
            {

                if (row.Code.Equals(code))
                {
                    this.SkillMap[code].IsSelected = value;
                    if (value)
                    {
                        addSelectDt(row);
                    }
                    else
                    {
                        delSelectDt(row);
                    }
                }
            }
        }

        private string getCurrImagePath()
        {
            DataGridViewRow currRow = dataGridView1.CurrentRow;
            if (currRow != null)
            {
                System.Console.WriteLine(currRow.Cells["path"].Value);
                if (currRow.Cells["path"].Value == null)
                {
                    return "";
                }
                string path = currRow.Cells["path"].Value.ToString();
                return path;
            }
            return "";
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {


        }

        private void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
        {

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            string code = dataGridView1.Rows[e.RowIndex].Cells["code"].Value.ToString();

            if (this.SkillMap.ContainsKey(code))
            {
                bool isChecked = this.SkillMap[code].IsSelected;
                if (!isChecked)
                {
                    this.SkillMap[code].IsSelected = true;
                    if (chkTpzh.Checked)
                    {

                        string combos = this.SkillMap[code].Combos;
                        foreach (string c in combos.Split(","))
                        {
                            this.combo(c, true);
                        }
                    }

                    addSelectDt(SkillMap[code]);
                }
            }

        }

        private void addSelectDt(Skill skill)
        {
            DataRow row = this.SelectDt.Rows.Add();
            row["code"] = skill.Code;
            row["name"] = skill.Name;
            row["weight"] = skill.Weight;
        }

        private void delSelectDt(Skill skill)
        {
            DataRow[] rows = this.SelectDt.Select("code = '" + skill.Code + "'");

            if (rows.Length > 0)
            {
                rows[0].Delete();
            }
        }

        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //string code = dataGridView2.Rows[e.RowIndex].Cells["code1"].Value.ToString();

            //if (this.SkillMap.ContainsKey(code))
            //{
            //    bool isChecked = this.SkillMap[code].IsSelected;
            //    if (isChecked)
            //    {
            //        this.SkillMap[code].IsSelected = false;
            //        if (chkTpzh.Checked)
            //        {

            //            string combos = this.SkillMap[code].Combos;
            //            foreach (string c in combos.Split(","))
            //            {
            //                this.combo(c, false);
            //            }
            //        }

            //        delSelectDt(SkillMap[code]);
            //    }
            //}
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DataGridViewRow row = dataGridView2.CurrentRow;

            if (row != null)
            {
                string code = row.Cells["code1"].Value.ToString();

                if (this.SkillMap.ContainsKey(code))
                {
                    bool isChecked = this.SkillMap[code].IsSelected;
                    if (isChecked)
                    {
                        this.SkillMap[code].IsSelected = false;
                        if (chkTpzh.Checked)
                        {

                            string combos = this.SkillMap[code].Combos;
                            foreach (string c in combos.Split(","))
                            {
                                this.combo(c, false);
                            }
                        }

                        delSelectDt(SkillMap[code]);
                    }
                }
            }
        }
    }

    class Skill
    {
        private string name = string.Empty;
        private string code = string.Empty;
        private string combos = string.Empty;
        private string imgPath = string.Empty;
        private int weight;
        private bool isSelected = false;

        public string Code { get => code; set => code = value; }
        public string Name { get => name; set => name = value; }
        public string Combos { get => combos; set => combos = value; }
        public string ImgPath { get => imgPath; set => imgPath = value; }
        public int Weight { get => weight; set => weight = value; }
        public bool IsSelected { get => isSelected; set => isSelected = value; }
    }
}
